using System.Collections;
using UnityEngine;

public class CarInShopManager : MonoBehaviour
{
	public static CarInShopManager instance;

	public bool active;

	private GameObject _currentCar;

	public GameObject[] shadows;

	public float timeDisappearance = 0.4f;

	[Range(1.1f, 4f)]
	public float pow = 2.7f;

	private string loadedCarName;

	private IEnumerator loadCoroutine;

	private IEnumerator disappearanceCorotine;

	private void Awake()
	{
		instance = this;
	}

	private void OnDestroy()
	{
		instance = null;
	}

	public void OnEnable()
	{
		active = true;
	}

	private void OnDisable()
	{
		active = false;
	}

	public void InstantiateCarInShop(string key)
	{
		if (!(loadedCarName == key))
		{
			if (loadCoroutine != null)
			{
				StopCoroutine(loadCoroutine);
			}
			loadCoroutine = InstantiateCarIEnumerator(key);
			if (base.gameObject.activeSelf)
			{
				StartCoroutine(loadCoroutine);
			}
		}
	}

	public bool isShopCar(CarBehavior car)
	{
		return car.gameObject == _currentCar;
	}

	private IEnumerator InstantiateCarIEnumerator(string key)
	{
		ResourceRequest tmpCar = Resources.LoadAsync("Cars/PrivateCars/" + key);
		if (_currentCar != null && disappearanceCorotine == null)
		{
			StartDisappearance();
		}
		yield return tmpCar;
		while (disappearanceCorotine != null)
		{
			yield return null;
		}
		StoptDisappearance();
		_currentCar = (GameObject)Object.Instantiate(tmpCar.asset, base.transform.position, base.transform.rotation);
		NJGMapItem item = _currentCar.GetComponent<NJGMapItem>();
		item.inMagazine = true;
		item.enabled = false;
		CarBehavior carBehavior = _currentCar.GetComponent<CarBehavior>();
		carBehavior.RCCcar.EnableCar(true);
		carBehavior.SwitchTexture(carBehavior.GetCurrentSkin());
		carBehavior.RemoveFromList();
		carBehavior.shopCar = true;
		carBehavior.Disable();
		carBehavior.RCCcar.ToggleVehicleComponents(false);
		visibleObjPhoton visibleObj = _currentCar.GetComponentInChildren<visibleObjPhoton>();
		if (visibleObj != null)
		{
			visibleObj.enabled = false;
		}
		_currentCar.GetComponentInChildren<PhotonTransformView>().enabled = false;
		_currentCar.GetComponent<ActivateCar>().enabled = false;
		_currentCar.transform.parent = base.transform;
		_currentCar.transform.localScale = Vector3.one;
		Component[] temp = _currentCar.GetComponentsInChildren<Transform>();
		for (int j = 0; j < temp.Length; j++)
		{
			temp[j].gameObject.layer = 26;
		}
		Rigidbody rigidbody = _currentCar.GetComponent<Rigidbody>();
		if (rigidbody != null)
		{
			rigidbody.isKinematic = true;
		}
		_currentCar.transform.localScale = _currentCar.GetComponent<CarBehavior>().shopScale;
		ParticleSystem[] particleSystems = carBehavior.GetComponentsInChildren<ParticleSystem>();
		SwitchShadow(carBehavior.currentShadowType);
		for (int i = 0; i < particleSystems.Length; i++)
		{
			particleSystems[i].startSize *= _currentCar.GetComponent<CarBehavior>().carParticlesScaler;
			particleSystems[i].startSpeed *= _currentCar.GetComponent<CarBehavior>().carParticlesScaler;
		}
		loadCoroutine = null;
		loadedCarName = key;
	}

	private IEnumerator CarDisappearance()
	{
		float time = 0f;
		while (time < timeDisappearance)
		{
			time += Time.unscaledDeltaTime;
			base.transform.localPosition = Vector3.right * 7f * Mathf.Pow(time / timeDisappearance, pow);
			yield return null;
		}
		disappearanceCorotine = null;
	}

	private void StartDisappearance()
	{
		disappearanceCorotine = CarDisappearance();
		StartCoroutine(disappearanceCorotine);
	}

	private void StoptDisappearance()
	{
		if (disappearanceCorotine != null)
		{
			StopCoroutine(disappearanceCorotine);
			disappearanceCorotine = null;
		}
		if (_currentCar != null)
		{
			Object.Destroy(_currentCar);
		}
		base.transform.localPosition = Vector3.zero;
	}

	private void SwitchShadow(CarShadowType shadowType)
	{
		for (int i = 0; i < shadows.Length; i++)
		{
			shadows[i].SetActive(false);
		}
		if ((bool)shadows[(int)shadowType])
		{
			shadows[(int)shadowType].SetActive(true);
		}
	}
}
